package com.fzu.normalservice.controller;


import com.fzu.commonutil.entity.Demand;
import com.fzu.commonutil.util.JwtUtil;
import com.fzu.commonutil.util.R;
import com.fzu.commonutil.params.DemandMatchParam;
import com.fzu.normalservice.param.DemandParam;
import com.fzu.normalservice.service.DemandService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/demand")
@RequiredArgsConstructor
public class DemandController {
    private final DemandService demandService;

    /**
     * 获取分页列表
     * @param demand
     * @param pageNum
     * @param pageSize
     * @param token
     * @return
     */
    @GetMapping
    public R page(Demand demand,
                  @RequestParam(required = false, defaultValue = "10") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "1") Integer pageSize,
                  @RequestHeader("token") String token) {
        return R.ok().data(demandService.page(demand, JwtUtil.getUser(token), pageNum, pageSize));
    }

    /**
     * 获取需求详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {
        return R.ok().data(demandService.detail(id));
    }

    /**
     * 获取需求信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        return R.ok().data(demandService.getById(id));
    }

    /**
     * 获取匹配列表
     * @param id
     * @return
     */
    @GetMapping("/match-list/{id}")
    public R matchList(@PathVariable("id") Integer id) {
        return R.ok().data(demandService.matchList(id));
    }

    /**
     * 发布需求
     * @param param
     * @param token
     * @return
     */
    @PostMapping
    public R add(@RequestBody DemandParam param,
                 @RequestHeader("token") String token) {
        Integer id = demandService.add(param, JwtUtil.getUser(token));
        try {
            demandService.demandMatch(id);
        } catch (Exception e) {
            System.out.println("需求匹配失败: " + id);
        }
        return R.ok();
    }

    /**
     * 取消
     * @param id
     * @param token
     * @return
     */
    @PutMapping("/cancel/{id}")
    public R cancel(@PathVariable("id") Integer id,
                    @RequestHeader("token") String token) {
        demandService.cancel(id, JwtUtil.getUser(token));
        return R.ok();
    }


    /**
     * 重新匹配
     * @param id
     * @param token
     * @return
     */
    @PutMapping("/flush/{id}")
    public R flush(@PathVariable("id") Integer id,
                   @RequestHeader("token") String token) {
        demandService.flush(id, JwtUtil.getUser(token));
        return R.ok();
    }

    /**
     * 需求完成匹配
     * @param param
     * @return
     */
    @PutMapping("/match")
    public R match(@RequestBody DemandMatchParam param) {
        demandService.match(param);
        return R.ok();
    }

}

